import axios from 'axios'
import { Message } from 'element-ui'
// import NProgress from 'nprogress'
// import 'nprogress/nprogress.css'

// NProgress.configure({showSpinner: false})

export const appBaseUrl = process.env.VUE_APP_ENV === 'local' ? 'api/' : process.env.VUE_APP_BASE_URL

const http = axios.create({
  timeout: 20000,
  headers: {
    // 'Content-Type': 'application/x-www-form-urlencoded',
    "Content-Type": "application/json",
  },
})

http.interceptors.request.use(
  config => {
    // NProgress.start()
    const _config = config
    _config.url = appBaseUrl + _config.url
    return _config
  },
  error => {
    return Promise.reject(error)
  }
)

http.interceptors.response.use(
  response => {
    if (response.data.code === 200) {
      return response.data.data
    } else {
      Message.error(response.data.msg)
      return Promise.reject(response.data)
    }
  },
  error => {
    return Promise.reject(error)
  }
)

http.get = (url) => {
  return http({
    method: 'get',
    url
  })
}

http.post = (url, data) => {
  return http({
    method: 'post',
    url,
    data
  })
}

export default http
